/*
 * idle_protection_plot.cpp
 *
 *  Created on: 30.07.2013
 *      Author: SpirTanol
 */

#include "ui/idle_protection_plot.h"
#include "global.h"

// QWT
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_picker.h>
#include <qwt_scale_widget.h>

// Qt
#include <QDebug>

// STR
#include <algorithm>

IdleProtectionPlot::IdleProtectionPlot(double curNom, double curMin, int volMode, double coeff)
{
	setAutoReplot(false);
	plotLayout()->setAlignCanvasToScales(true);
	QwtLegend *legend = new QwtLegend();
	legend->setItemMode(QwtLegend::ReadOnlyItem);
	insertLegend(legend, QwtPlot::RightLegend);
	setMinimumWidth(220);

	setAxisLabelRotation(QwtPlot::xBottom, 0.0);
	setAxisFont(QwtPlot::xBottom, QFont("Arial", 8));
	setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignHCenter | Qt::AlignBottom);

	QwtText text(trUtf8("Минимально допустимый ток, А"));
	text.setFont(QFont("Arial", 8));
	setAxisTitle(QwtPlot::yLeft, text);

	text = trUtf8("Напряжение сети, В");
	text.setFont(QFont("Arial", 8));
	setAxisTitle(QwtPlot::xBottom, text);

	setCanvasBackground(QColor(240, 241, 239));

	QwtPlotCurve *curve;
	curve = new QwtPlotCurve(trUtf8("Минимально\nдопустимый\nток"));
	curve->setPen(QColor("#FF9933"));
	curve->attach(this);
	curve->show();

	QwtPlotGrid *grid = new QwtPlotGrid;
	grid->enableY(true);
	grid->enableYMin(true);
	grid->enableX(true);
	grid->enableXMin(false);
	grid->setMajPen(QPen(Qt::blue, 0, Qt::DotLine));
	grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	grid->attach(this);

	class Picker : public QwtPlotPicker
	{
	public:
		Picker(int x, int y, int flags, RubberBand rb, DisplayMode dm, QwtPlotCanvas *c) : QwtPlotPicker(x, y, flags, rb, dm, c) {}
		~Picker() {}

		QwtText trackerText(const QPoint &pos) const
		{
			QString str = QString::fromUtf8("Ток: ") + QString::number(plot()->invTransform(QwtPlot::yLeft, pos.y()), 'f', 1) +
					QString::fromUtf8(" Напряжение: ") + QString::number(plot()->invTransform(QwtPlot::xBottom, pos.x()), 'f', 1);
			return str;
		}
	};

	new Picker(QwtPlot::xBottom, QwtPlot::yLeft,
	QwtPicker::PointSelection,
	QwtPicker::CrossRubberBand, QwtPicker::AlwaysOn, canvas());

	static const double volMinMax[4][2] =
	{
			{187.0, 242.0},
			{176.0, 253.0},
			{165.0, 264.0},
			{154.0, 275.0},
	};

	QVector<double> vol;
	QVector<double> cur;
	const double volNom = 220.0;

	vol.push_back(volMinMax[volMode][0]);
	vol.push_back(volMinMax[volMode][1]);

	cur.push_back(curNom * curMin * (1.0 + (volMinMax[volMode][0] - volNom) / volNom * coeff));
	cur.push_back(curNom * curMin * (1.0 + (volMinMax[volMode][1] - volNom) / volNom * coeff));

	setAxisScale(QwtPlot::xBottom, volMinMax[volMode][0], volMinMax[volMode][1]);
	setAxisScale(QwtPlot::yLeft, std::min<double>(cur.front(), cur.back()) - 0.1,
			std::max<double>(cur.front(), cur.back()) + 0.1);

	curve->setData(vol, cur);

	replot();
}

IdleProtectionPlot::~IdleProtectionPlot()
{

}
